/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package MuleGame;
package MuleGUI;
import java.awt.*;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import MuleBackend.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

/**
 *The GUI for instantiating each player.
 *NameGUI takes in input from user to create the player object
 * @author Spyro
 */
public class NameGUI extends javax.swing.JFrame {
	private JPanel mainPanel;
	private JLabel playerLabel, raceLabel, colorLabel, okLabel;
	private JTextField nameTextField;
//	private JButton okButton;
	ImageIcon playerImage;
	Color playerColor;
	int numPlayer;
	static int id = 1;
	static LinkedList<Color> colorChosen = new LinkedList<Color>();
	static LinkedList<String> nameChosen = new LinkedList<String>();
	
	int race = 1, color = 1;

    /**
     * Creates new form NameGUI
     * @param num the number of players
     */
    public NameGUI(int num) {
		numPlayer = num;
		setResizable(false);
		getContentPane().setLayout(null);
		buildGUI();
    }
    
    private void increaseID(){
    	id++;
    }

    public void setNumberOfPlayers(int num){
    	numPlayer = num;
    }

    /**
     * this method create the name
     */
	public void buildGUI(){
		setTitle("Player Option");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(389,610);
		setLocationRelativeTo(null);
		
		mainPanel = new JPanel();
		mainPanel.setBorder(new EmptyBorder(5,5,5,5));
		setContentPane(mainPanel);
		mainPanel.setLayout(null);
		
		playerLabel = new JLabel("Player");
		playerLabel.setBounds(10,11,81,49);
		playerLabel.setFont(new Font("Tahoma", Font.PLAIN, 24));
		nameTextField = new JTextField("Enter name");
		nameTextField.setFont(new Font("Tahoma", Font.PLAIN, 24));
		nameTextField.setBounds(101,6,215,60);
		
		raceLabel = new JLabel();
		raceLabel.setBounds(10,100,350,350);
		colorLabel = new JLabel();
		colorLabel.setBounds(147,453,70,70);
		
		okLabel = new JLabel("OK");
		okLabel.setFont(new Font("Times New Roman", Font.PLAIN, 28));
		okLabel.setBorder(new LineBorder(Color.BLACK));
		okLabel.setBounds(160, 534, 53, 38);
		
		getContentPane().add(playerLabel);
		getContentPane().add(nameTextField);
		getContentPane().add(raceLabel);
		getContentPane().add(colorLabel);
		getContentPane().add(okLabel);
		
		raceLabel.addMouseListener(new choose());
		colorLabel.addMouseListener(new choose());
		okLabel.addMouseListener(new choose());
		
	}                         
	/**
	 * this method display the player's character 
	 * according to the player's color
	 */
	public void paint(Graphics g){
		super.paint(g);
		int x = 20, y= 110;

		int x1 = 160,y1 = 485;
		
		playerColor = newColor();
		
		g.setColor(playerColor);
		
		playerImage = new ImageIcon("src/players/draw"+race+color+".png");
		g.drawImage(playerImage.getImage(), x, y, 350, 350, null);
		g.fillRect(x1,y1,60,60);
	}
	
	/**
	 * this method restrain players from choosing the 
	 * same color
	 * @return the new color
	 */
	private Color newColor(){
		Color temp = chooseColor();
		while(colorChosen.contains(temp)){
			++color;
			if (color > 4)color = 1;
			temp = chooseColor();
		}
		return temp;
	}
	
	/**
	 * this method find the color represented
	 * by the color number
	 * @return the color that represent the color number
	 */
	private Color chooseColor(){
		Color colorTemp = null;
		switch (color){
		case 1:
			colorTemp = Color.BLUE;
			break;
		case 2:
			colorTemp = Color.RED;
			break;
		case 3:
			colorTemp = Color.GREEN;
			break;
		case 4:
			colorTemp = Color.YELLOW;
			break;
		}
		return colorTemp;
	}
	
	public boolean nameValid(String name){
		if (name == null || name == "")return true;
		if (name.length() > 11) return true;
		if (nameChosen.contains(name))return true;
		if (name.replaceAll("\\s","") == "")return true;
		return false;
	}
	
	/**
	 * this class allow player to click on the images to 
	 * change the images
	 * @author Spyro
	 *
	 */
	private class choose extends MouseAdapter{
		
		public void mouseClicked(MouseEvent e){
			JLabel label = (JLabel)e.getSource();
			if (label == raceLabel){
				if (++race > 4) race = 1;
			}
			if (label == colorLabel){
				if (++color > 4) color = 1;
			}
//			JButton button = (JButton)e.getSource();
			if (okLabel == label){
				String name = nameTextField.getText();
				while (nameValid(name)){
					name = JOptionPane.showInputDialog("Please enter different name!","Enter name");
				}
				Player player = new Player(name,playerImage,playerColor,id);
				GameDriver.game.addPlayer(player);
				GameDriver.game.addImages(playerImage);
				colorChosen.add(playerColor);
				nameChosen.add(name);
				increaseID();
        		System.out.print(player.toString());
        		setVisible(false);
        		if (--numPlayer>0){
        			new NameGUI(numPlayer).setVisible(true);
        		}

        		else {
        			dispose();
        			GameDriver.game.createMap();
    				GameDriver.game.startStage();
        		}
			}
			repaint();
		}
	}

    // End of variables declaration                   
}